<%@page import="com.trw.trp.bean.*" %>

<%@ include file="header.jsp"%>

<%
	boolean isSuper = cUser.getRoleList().contains(RoleBean.SUPER_ADMIN_ROLE);
%>

<div id="userDiv"/>

<script type="text/javascript">

function doDelUser(userId,userName,email){
	Ext.MessageBox.show({
		title:'Confirm',
		msg: 'Do you want to delete user['+userName+']? If yes, please enter the reason:',
	    width:300,
	    buttons: Ext.MessageBox.OKCANCEL,
	    multiline: true,
	    fn: function(btn,text){
			if(btn == 'ok'){
				var grid = Ext.getCmp('userMgtGrid');
				grid.getEl().mask('deleting...');
				Ext.Ajax.request({
					url: 'deleteUser.action',
					params: {
						userId:userId,
						userName:userName,
						to:email,
						cc:'<%=cUser.getEmail()%>',
						emailId:4,
						text:text
					},
					success: function(response){
						var result = Ext.util.JSON.decode(response.responseText);
						Ext.Msg.alert('Info', result.message);
						grid.getEl().unmask();
						grid.getStore().load();
						grid.getView().refresh();
					},
					failure: function(response){
						Ext.Msg.alert('Warning', response.responseText);
						grid.getEl().unmask();
					}
				});	
			}
		}
	});
}

Ext.onReady(function(){
var grid,userWin,userStore;

if(!grid){
	var store = new Ext.data.Store({
        autoLoad: false,
		fields:['userId', 'name','email'],
		proxy: {
			type: 'ajax',
			url: 'getUsers.action',
			reader: {
				type: 'json',
				root: 'items',
				totalProperty: 'size'
			}
		}
	});
	
	grid = Ext.create('Ext.grid.Panel', {
		title: 'All Users',
		forceFit: true,
		loadMask: true,
		autoScroll:true,
		id:'userMgtGrid',
		columns: [
			{ header: 'User Name',  dataIndex: 'name', width: 100},
			{ header: 'User Email',  dataIndex: 'email'},
			{
				width:10,
				renderer: function(value,a,obj,idx){
					var userId = store.getAt(idx).data.userId;
					var userName = store.getAt(idx).data.name;
					var email = store.getAt(idx).data.email;
					if(userId==<%=cUser.getUserId()%>){
						return '';
					}else{
						return '<img title="Delete User" src="images/delete.gif" onclick="doDelUser('+userId+',\''+userName+'\',\''+email+'\')"/>';
					}	
				}
			}],
		store: store,
		width: parseInt(Ext.getBody().getWidth())*0.98,
		height: Ext.getBody().getHeight()-100,
		renderTo: 'userDiv',
		listeners:{
			'itemdblclick':function(view,record,item,index,e,eOpts){
				Ext.Ajax.request({
					url: 'getUser.action',
					params: {
						userId: record.data.userId
					},
					success: function(response){
						var user = Ext.decode(response.responseText);
						var isSuper = false;
						if(user.roleList){
							for(var i=0;i<user.roleList.length;i++){
								if(user.roleList[i].roleId==2){
									isSuper = true;break;
								}
							}
						}
						if(!userWin){
							var userForm = Ext.create('Ext.form.Panel', {
								frame:true,
								bodyStyle:'padding:5px 5px 0',
								border:0,
								defaultType: 'textfield',
								defaults: {
									anchor: '100%',
									iconAlign:'top'
								},
								layout:{
									type:'table',
									columns:1
								},
								items:[{
									fieldLabel: 'User Email',
									id: 'email',
									name: 'email',
									allowBlank:false,
									width:400
								},{
									fieldLabel: 'User Name',
									id: 'name',
									name: 'name',
									allowBlank:false,
									width:400
								},{
									fieldLabel: 'Division',
									name: 'division',
									id: 'division',
									allowBlank:false,
									width:400
								},{
									fieldLabel: 'Department',
									name: 'department',
									id: 'department',
									allowBlank:false,
									width:400
								},{
									fieldLabel: 'Position',
									name: 'position',
									id: 'position',
									xtype:'combo',
									width:400,
									allowBlank:false,
									displayField: 'name',
									valueField: 'name',
									typeAhead:true,
									autoScroll:true,
									queryMode: 'local',
									store:  Ext.create('Ext.data.Store', {
										autoLoad:true,
										fields: ['name'],
										proxy: {
											type: 'ajax',
											url: 'getList.action?listId=8',
											reader: {
												type: 'json',
												root: 'itemList',
												totalProperty: 'size'
											}
										}
									})
								},{
									fieldLabel: 'Desk Tel(Dir)',
									name: 'tel',
									id: 'tel',
									allowBlank:false,
									width:400
								},{
									fieldLabel: 'Mobile',
									name: 'mobile',
									id: 'mobile',
									allowBlank:false,
									width:400
								},{
									xtype:'checkbox',
									boxLabel: 'Super Admin',
									name: 'isAdmin',
									id: 'isAdmin',
									inputValue: 'Y',
									width:400
								},{
									xtype:'hidden',
									id:'userId'
								}]
							});
							
							userWin = Ext.create('Ext.Window', {
								title: 'Modify User',
								width: 510,
								height: 300,
								border:0,
								modal:true,
								buttonAlign : "center",
								defaultType: 'textfield',
								defaults: {
									anchor: '100%'
								},
								items: userForm,
								closeAction: function(){
									userWin.hide();
								},
								buttons:[{
									text:'Save',
									id:'btnSave',
									handler:function(){
										if(!userForm.getForm().isValid()){
											Ext.MessageBox.alert('Warning','Please fill in all the fields!');
											return;
										}
										userWin.getEl().mask('saveing data...');
										userForm.getForm().submit({
											url: 'updateUser.action',
											params:{userId:Ext.getCmp('userId').getValue()},
											success: function(form, action){
												var result = action.result;
												if(result.data.state==0){
													flashMessage('data saved.','btnSave',1500);
												}else{
													Ext.MessageBox.alert('Warning', 'save failed,please try again later.');
												}
												userWin.getEl().unmask();
											}
										});
									}
								},{
									text:'Cancel',
									handler:function(){
										userWin.hide();
									}
								}]
							});
						}
						Ext.getCmp('email').setValue(user.email);
						Ext.getCmp('name').setValue(user.name);
						Ext.getCmp('division').setValue(user.division);
						Ext.getCmp('department').setValue(user.department);
						Ext.getCmp('position').setValue(user.position);
						Ext.getCmp('tel').setValue(user.tel);
						Ext.getCmp('mobile').setValue(user.mobile);
						<%if(isSuper){%>
							if(isSuper){
								Ext.getCmp('isAdmin').setValue('Y');
							}else{
								Ext.getCmp('isAdmin').setValue('');
							}
						<%}else{%>
							Ext.getCmp('isAdmin').setDisabled(true);
						<%}%>
						Ext.getCmp('userId').setValue(user.userId);
						userWin.show();
					}		
				});
			}
		}
	});
	
	store.load();
	grid.getView().refresh();
}

});
</script>

<%@ include file="footer.jsp"%>